<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 500px;
            height: 300px;
            background-color: pink;
            margin: 100px auto;
            position: relative;
        }

        .box .title {
            font-weight: bold;
            line-height: 80px;
            text-align: center;
        }

        .box .block {
            margin: 30px 0;
            box-sizing: border-box;
            padding: 0 50px;
        }

        .box .block input {
            height: 40px;
            padding-left: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: 100%;
            outline: none;
            box-sizing: border-box;
        }

        .box p {
            font-size: 12px;
            color: blue;
            position: absolute;
            right: 45px;
            bottom: 50px;
        }

        .box .block:nth-last-child(1) input {
            background-color: skyblue;
            color: white;
            font-weight: 700;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="title">登录页</div>
        <div class="block">
            <input type="text" placeholder="手机号/用户名/邮箱">
        </div>
        <div class="block">
            <input type="password" placeholder="密码">
        </div>
        <p>忘记密码？</p>
        <div class="block">
            <input type="button" value="登陆">
        </div>
    </div>
</body>

</html>
<script src="./promise-ajax.js"></script>
<script>
    //获取三个input输入框
    var inputs = document.querySelectorAll('.box .block input');
    //给button绑定点击事件
    inputs[2].onclick = function () {
        if (inputs[0].value && inputs[1].value) {
            // 检验第一个输入框的值，邮箱或者手机号，或用户名（字母数字下划线至少4位）&&校验密码
            if ((/^\w+@[0-9a-z]+\.(com|cn|org)$/.test(inputs[0].value) || /^1[3-9]\d{9}$/.test(inputs[0].value) || /^\w{4,}$/.test(inputs[0].value)) && (/^[0-9]{4}$/.test(inputs[1].value))) {
                //通过校验，调ajax函数
                ajax('get', 'https://yantianfeng.com/api/login', { username: inputs[0].value, password: inputs[1].value }).then((res) => {
                    if (res.status == 200) {
                        alert('登陆成功');
                    } else {
                        alert('登陆失败!');
                    }
                })

            } else {
                alert('请输入正确的手机号/用户名/密码');
            }
        } else {
            alert('用户名/密码不能为空!');
        }
    }
</script>